package o2o.thridpost.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author sunyufan deporation@qq.com
 * @date 2021/4/30 10:25
 * @description: ObjectToMap 对象转map
 */
@Slf4j
public class ObjectToMap {
    public static Map<String, Object> toMap(Object object){
        Map<String,Object> map = new HashMap<>();
        Class<?> clazz = object.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                map.put(field.getName(),field.get(object));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                log.error(e.toString() + ":" + e.getMessage());
            }
        }
        return map;
    }

}
